package com.ruoyi.bus.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.bus.domain.CustBase;
import com.ruoyi.bus.service.ICustBaseService;
import com.ruoyi.bus.service.NoGenerateServlce;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.CustAddr;
import com.ruoyi.bus.service.ICustAddrService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地址簿Controller
 *
 * @author ruoyi
 * @date 2020-04-09
 */
@Controller
@RequestMapping("/bus/custAddr")
public class CustAddrController extends BaseController {
    private String prefix = "bus/custAddr";

    @Autowired
    private ICustAddrService custAddrService;

    @Autowired
    private ICustBaseService custBaseService;

    @Autowired
    private NoGenerateServlce noGenerateServlce;

    @RequiresPermissions("bus:custAddr:view")
    @GetMapping()
    public String custAddr() {
        return prefix + "/custAddr";
    }

    /**
     * 查询地址簿列表
     */
    @RequiresPermissions("bus:custAddr:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustAddr custAddr) {
        startPage();
        List<CustAddr> addrList=custAddrService.list(new QueryWrapper<>(custAddr));
        for (CustAddr addr : addrList) {
            CustBase custBase=custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(addr.getCustomerId())));
            if(custBase!=null){
                addr.setCustomerName(custBase.getCustomerName());
            }
        }
        return getDataTable(addrList);
    }

    /**
     * 导出地址簿列表
     */
    @RequiresPermissions("bus:custAddr:export")
    @Log(title = "地址簿", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustAddr custAddr) {
        List<CustAddr> list = custAddrService.list(new QueryWrapper<>(custAddr));
        ExcelUtil<CustAddr> util = new ExcelUtil<>(CustAddr.class);
        return util.exportExcel(list, "custAddr");
    }



    /**
     * 新增地址簿
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        CustAddr custAddr = new CustAddr();
        custAddr.setAddrId(noGenerateServlce.getSeqNo("AD",7));
        mmap.put("custAddr", custAddr);
        return prefix + "/edit";
    }

    /**
     * 新增保存地址簿
     */
    @RequiresPermissions("bus:custAddr:add")
    @Log(title = "地址簿", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CustAddr custAddr) {
        return toAjax(custAddrService.save(custAddr));
    }

    /**
     * 修改地址簿
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        CustAddr custAddr = custAddrService.getById(id);
        CustBase custBase=custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(custAddr.getCustomerId())));
        if(custBase!=null){
            custAddr.setCustomerName(custBase.getCustomerName());
        }
        mmap.put("custAddr", custAddr);
        return prefix + "/edit";
    }

    /**
     * 修改保存地址簿
     */
    @RequiresPermissions("bus:custAddr:edit")
    @Log(title = "地址簿", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CustAddr custAddr) {
        return toAjax(custAddrService.updateById(custAddr));
    }

    /**
     * 删除地址簿
     */
    @RequiresPermissions("bus:custAddr:remove")
    @Log(title = "地址簿", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(custAddrService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
